﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{
	/// <summary>
	/// Summary description for frmChucv.
	/// </summary>
	public class frmChucDanh : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		/// 
        private AccessData mDB = new AccessData();
        Language lan = new Language();
		private DataSet ds=new DataSet();
        int flag = 0;
		private System.Windows.Forms.DataGrid dgrDanhMuc1;
		private System.Windows.Forms.TextBox txtTen;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtMa;
		private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Panel panel1;
        private ComboBox cmdNhombc;
        private Label label3;
        private TextBox txtsott;
        private Label lbsott;
        private TextBox txttenvt;
        private Label label4;
        private Button btn_them;
        private Button btn_sua;
        private Button btn_luu;
        private Button btn_boqua;
        private Button btn_huy;
        private Button btn_thoat;
		private System.ComponentModel.Container components = null;

		public frmChucDanh()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmChucDanh));
            this.dgrDanhMuc1 = new System.Windows.Forms.DataGrid();
            this.txtTen = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtMa = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btn_thoat = new System.Windows.Forms.Button();
            this.btn_huy = new System.Windows.Forms.Button();
            this.btn_boqua = new System.Windows.Forms.Button();
            this.btn_luu = new System.Windows.Forms.Button();
            this.btn_sua = new System.Windows.Forms.Button();
            this.btn_them = new System.Windows.Forms.Button();
            this.cmdNhombc = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtsott = new System.Windows.Forms.TextBox();
            this.lbsott = new System.Windows.Forms.Label();
            this.txttenvt = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc1)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // dgrDanhMuc1
            // 
            this.dgrDanhMuc1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrDanhMuc1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrDanhMuc1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrDanhMuc1.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrDanhMuc1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc1.CaptionText = "Danh mục chức danh";
            this.dgrDanhMuc1.CaptionVisible = false;
            this.dgrDanhMuc1.DataMember = "";
            this.dgrDanhMuc1.FlatMode = true;
            this.dgrDanhMuc1.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrDanhMuc1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrDanhMuc1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrDanhMuc1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc1.LinkColor = System.Drawing.Color.Teal;
            this.dgrDanhMuc1.Location = new System.Drawing.Point(8, 8);
            this.dgrDanhMuc1.Name = "dgrDanhMuc1";
            this.dgrDanhMuc1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc1.RowHeaderWidth = 10;
            this.dgrDanhMuc1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrDanhMuc1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc1.Size = new System.Drawing.Size(464, 247);
            this.dgrDanhMuc1.TabIndex = 12;
            this.dgrDanhMuc1.CurrentCellChanged += new System.EventHandler(this.dgrDanhMuc1_CurrentCellChanged);
            // 
            // txtTen
            // 
            this.txtTen.BackColor = System.Drawing.Color.White;
            this.txtTen.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtTen.Location = new System.Drawing.Point(75, 280);
            this.txtTen.MaxLength = 50;
            this.txtTen.Name = "txtTen";
            this.txtTen.Size = new System.Drawing.Size(393, 20);
            this.txtTen.TabIndex = 1;
            this.txtTen.TextChanged += new System.EventHandler(this.txtTen_TextChanged);
            this.txtTen.Validated += new System.EventHandler(this.txtTen_Validated);
            this.txtTen.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtTen_KeyDown);
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.SystemColors.Control;
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(72, 263);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(86, 13);
            this.label2.TabIndex = 11;
            this.label2.Text = "Tên chức danh :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtMa
            // 
            this.txtMa.BackColor = System.Drawing.Color.White;
            this.txtMa.Enabled = false;
            this.txtMa.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtMa.Location = new System.Drawing.Point(10, 280);
            this.txtMa.Name = "txtMa";
            this.txtMa.Size = new System.Drawing.Size(59, 20);
            this.txtMa.TabIndex = 0;
            this.txtMa.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtMa_KeyDown);
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.Control;
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(7, 263);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(28, 13);
            this.label1.TabIndex = 10;
            this.label1.Text = "Mã :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btn_thoat);
            this.panel1.Controls.Add(this.btn_huy);
            this.panel1.Controls.Add(this.btn_boqua);
            this.panel1.Controls.Add(this.btn_luu);
            this.panel1.Controls.Add(this.btn_sua);
            this.panel1.Controls.Add(this.btn_them);
            this.panel1.Location = new System.Drawing.Point(17, 347);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(451, 45);
            this.panel1.TabIndex = 6;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            // 
            // btn_thoat
            // 
            this.btn_thoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_thoat.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_thoat.Image = global::Human.Properties.Resources.close_r;
            this.btn_thoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_thoat.Location = new System.Drawing.Point(363, 7);
            this.btn_thoat.Name = "btn_thoat";
            this.btn_thoat.Size = new System.Drawing.Size(60, 28);
            this.btn_thoat.TabIndex = 12;
            this.btn_thoat.Text = "&Thoát";
            this.btn_thoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_thoat.UseVisualStyleBackColor = true;
            this.btn_thoat.Click += new System.EventHandler(this.btn_thoat_Click);
            // 
            // btn_huy
            // 
            this.btn_huy.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_huy.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_huy.Image = global::Human.Properties.Resources.Cancel;
            this.btn_huy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_huy.Location = new System.Drawing.Point(297, 7);
            this.btn_huy.Name = "btn_huy";
            this.btn_huy.Size = new System.Drawing.Size(60, 28);
            this.btn_huy.TabIndex = 11;
            this.btn_huy.Text = "&Hủy";
            this.btn_huy.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_huy.UseVisualStyleBackColor = true;
            this.btn_huy.Click += new System.EventHandler(this.btn_huy_Click);
            // 
            // btn_boqua
            // 
            this.btn_boqua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_boqua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_boqua.Image = global::Human.Properties.Resources.t_boqua1;
            this.btn_boqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_boqua.Location = new System.Drawing.Point(221, 7);
            this.btn_boqua.Name = "btn_boqua";
            this.btn_boqua.Size = new System.Drawing.Size(70, 28);
            this.btn_boqua.TabIndex = 10;
            this.btn_boqua.Text = "&Bỏ qua";
            this.btn_boqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_boqua.UseVisualStyleBackColor = true;
            this.btn_boqua.Click += new System.EventHandler(this.btn_boqua_Click);
            // 
            // btn_luu
            // 
            this.btn_luu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_luu.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_luu.Image = global::Human.Properties.Resources.save;
            this.btn_luu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_luu.Location = new System.Drawing.Point(155, 7);
            this.btn_luu.Name = "btn_luu";
            this.btn_luu.Size = new System.Drawing.Size(60, 28);
            this.btn_luu.TabIndex = 9;
            this.btn_luu.Text = "&Lưu";
            this.btn_luu.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_luu.UseVisualStyleBackColor = true;
            this.btn_luu.Click += new System.EventHandler(this.btn_luu_Click);
            // 
            // btn_sua
            // 
            this.btn_sua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_sua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_sua.Image = global::Human.Properties.Resources.quick_edit;
            this.btn_sua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_sua.Location = new System.Drawing.Point(89, 7);
            this.btn_sua.Name = "btn_sua";
            this.btn_sua.Size = new System.Drawing.Size(60, 28);
            this.btn_sua.TabIndex = 8;
            this.btn_sua.Text = "&Sửa";
            this.btn_sua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_sua.UseVisualStyleBackColor = true;
            this.btn_sua.Click += new System.EventHandler(this.btn_sua_Click);
            // 
            // btn_them
            // 
            this.btn_them.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_them.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_them.Image = global::Human.Properties.Resources.add;
            this.btn_them.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_them.Location = new System.Drawing.Point(23, 7);
            this.btn_them.Name = "btn_them";
            this.btn_them.Size = new System.Drawing.Size(60, 28);
            this.btn_them.TabIndex = 7;
            this.btn_them.Text = "&Thêm";
            this.btn_them.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_them.UseVisualStyleBackColor = true;
            this.btn_them.Click += new System.EventHandler(this.btn_them_Click);
            // 
            // cmdNhombc
            // 
            this.cmdNhombc.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmdNhombc.FormattingEnabled = true;
            this.cmdNhombc.Location = new System.Drawing.Point(327, 320);
            this.cmdNhombc.Name = "cmdNhombc";
            this.cmdNhombc.Size = new System.Drawing.Size(141, 21);
            this.cmdNhombc.TabIndex = 4;
            this.cmdNhombc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cmdNhombc_KeyDown);
            // 
            // label3
            // 
            this.label3.BackColor = System.Drawing.SystemColors.Control;
            this.label3.ForeColor = System.Drawing.Color.DarkBlue;
            this.label3.Location = new System.Drawing.Point(324, 303);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(38, 13);
            this.label3.TabIndex = 15;
            this.label3.Text = "Nhóm:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtsott
            // 
            this.txtsott.BackColor = System.Drawing.Color.White;
            this.txtsott.Enabled = false;
            this.txtsott.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtsott.Location = new System.Drawing.Point(12, 321);
            this.txtsott.MaxLength = 2;
            this.txtsott.Name = "txtsott";
            this.txtsott.Size = new System.Drawing.Size(59, 20);
            this.txtsott.TabIndex = 2;
            this.txtsott.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtsott_KeyDown);
            // 
            // lbsott
            // 
            this.lbsott.BackColor = System.Drawing.SystemColors.Control;
            this.lbsott.ForeColor = System.Drawing.Color.DarkBlue;
            this.lbsott.Location = new System.Drawing.Point(9, 304);
            this.lbsott.Name = "lbsott";
            this.lbsott.Size = new System.Drawing.Size(53, 13);
            this.lbsott.TabIndex = 12;
            this.lbsott.Text = "Số thứ tự:";
            this.lbsott.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txttenvt
            // 
            this.txttenvt.BackColor = System.Drawing.Color.White;
            this.txttenvt.Enabled = false;
            this.txttenvt.ForeColor = System.Drawing.Color.DarkBlue;
            this.txttenvt.Location = new System.Drawing.Point(75, 321);
            this.txttenvt.MaxLength = 50;
            this.txttenvt.Name = "txttenvt";
            this.txttenvt.Size = new System.Drawing.Size(246, 20);
            this.txttenvt.TabIndex = 3;
            this.txttenvt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txttenvt_KeyDown);
            // 
            // label4
            // 
            this.label4.BackColor = System.Drawing.SystemColors.Control;
            this.label4.ForeColor = System.Drawing.Color.DarkBlue;
            this.label4.Location = new System.Drawing.Point(72, 304);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(67, 13);
            this.label4.TabIndex = 14;
            this.label4.Text = "Tên viết tắt :";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // frmChucDanh
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(480, 395);
            this.Controls.Add(this.txttenvt);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.cmdNhombc);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.txtTen);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.lbsott);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtsott);
            this.Controls.Add(this.txtMa);
            this.Controls.Add(this.dgrDanhMuc1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmChucDanh";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Chức danh";
            this.Load += new System.EventHandler(this.frmChucv_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc1)).EndInit();
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmChucv_Load(object sender, System.EventArgs e)
		{
			chucvu();
            //
            cmdNhombc.DisplayMember = "ten";
            cmdNhombc.ValueMember = "ma";
            cmdNhombc.DataSource = mDB.get_data("select * from "+mDB.user+".nhomcd ").Tables[0];
            cmdNhombc.SelectedIndex = -1;
			Ena_control(true);
			refresh_Text();
			Ena_textbox(false);
			btn_them.Focus();           
		}
		private void chucvu()
		{
			DataSet ds1=new DataSet();
            ds1 = mDB.get_data("SELECT a.ma,a.ten,a.tenvt,a.nhombc,a.thutu,b.ten as tenhombc FROM " + mDB.user + ".dmcd a left join " + mDB.user + ".nhomcd b on a.nhombc=b.ma order by a.thutu"); 
			dgrDanhMuc1.DataSource=null;
			dgrDanhMuc1.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = ds1.Tables[0].TableName;
		
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "ma";
			TextCol1.HeaderText = lan.Change_language_MessageText("Mã chức danh");
			TextCol1.Width = 100;
			TextCol1.ReadOnly=true;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrDanhMuc1.TableStyles.Add(ts);
		
			DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
			TextCol2.MappingName = "ten";
			TextCol2.HeaderText = lan.Change_language_MessageText("Tên chức danh");
			TextCol2.Width = 180;
			TextCol2.ReadOnly=true;
			TextCol2.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol2);
			dgrDanhMuc1.TableStyles.Add(ts);

            DataGridTextBoxColumn TextCol3 = new DataGridTextBoxColumn();
            TextCol3 = new DataGridTextBoxColumn();
            TextCol3.MappingName = "tenvt";
            TextCol3.HeaderText = lan.Change_language_MessageText("Tên viết tắt");
            TextCol3.Width = 100;
            TextCol3.ReadOnly = true;
            TextCol3.Alignment = HorizontalAlignment.Left;
            ts.GridColumnStyles.Add(TextCol3);
            dgrDanhMuc1.TableStyles.Add(ts);

            DataGridTextBoxColumn TextCol4 = new DataGridTextBoxColumn();
            TextCol4 = new DataGridTextBoxColumn();
            TextCol4.MappingName = "tenhombc";
            TextCol4.HeaderText = lan.Change_language_MessageText("Nhóm báo cáo");
            TextCol4.Width = 80;
            TextCol4.ReadOnly = true;
            TextCol4.Alignment = HorizontalAlignment.Left;
            ts.GridColumnStyles.Add(TextCol4);
            dgrDanhMuc1.TableStyles.Add(ts);

            DataGridTextBoxColumn TextCol5 = new DataGridTextBoxColumn();
            TextCol5 = new DataGridTextBoxColumn();
            TextCol5.MappingName = "thutu";
            TextCol5.HeaderText = lan.Change_language_MessageText("Số TT");
            TextCol5.Width = 40;
            TextCol5.ReadOnly = true;
            TextCol5.Alignment = HorizontalAlignment.Left;
            ts.GridColumnStyles.Add(TextCol5);
            dgrDanhMuc1.TableStyles.Add(ts);
		
			ts.DataGrid.DataSource=ds1.Tables[0];
            CurrencyManager cm = (CurrencyManager)BindingContext[dgrDanhMuc1.DataSource, dgrDanhMuc1.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowNew = false;
            dv.AllowEdit = false;
            dv.AllowDelete = false;
		}	
		private void Ena_control(bool ena)
		{
			btn_them.Enabled=ena;
			btn_sua.Enabled=ena;
			btn_luu.Enabled=!ena;
			btn_boqua.Enabled=!ena;
			btn_huy.Enabled=ena;
			btn_thoat.Enabled=ena;

		}
		private void Ena_textbox(bool ena)
		{
            txtsott.Enabled = ena;
            txtTen.Enabled=ena;
            cmdNhombc.Enabled = ena;
            txttenvt.Enabled = ena;
		}
		private void clear_Text()
		{			
			txtTen.Clear();
            txttenvt.Text = "";
            txtsott.Text = "";
            cmdNhombc.SelectedIndex = -1;
		}
		private void refresh_Text()
		{
			int i=dgrDanhMuc1.CurrentRowIndex;
			if(i>=0)
			{
				txtMa.Text=dgrDanhMuc1[i,0].ToString();
				txtTen.Text=dgrDanhMuc1[i,1].ToString();
                if (!dgrDanhMuc1[i, 3].ToString().Trim().Equals(""))
                    cmdNhombc.Text = dgrDanhMuc1[i, 3].ToString();
                else
                    cmdNhombc.SelectedIndex = -1;
                txtsott.Text = dgrDanhMuc1[i, 4].ToString();
                txttenvt.Text = dgrDanhMuc1[i, 2].ToString();
			}
		}	
		
		private void btnKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void dgrDanhMuc1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				refresh_Text();
				Ena_textbox(false);
				Ena_control(true);
			}
			catch{}
		}
	
        private bool kemtra(string macd)
        {
            bool flag = false;
            string t = mDB.get_data("select count(*) as tong from " + mDB.user + ".llnv where macd='" + macd + "'").Tables[0].Rows[0][0].ToString();
            try
            {
                if (Convert.ToInt32(t) > 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            catch
            {
                flag = false;
            }
            if (flag)
                MessageBox.Show(lan.Change_language_MessageText("Chức vụ đã sử dụng không thể hủy!"),AccessData.Msg);
            return flag;

        }
	
		private void txtTen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter)
			{
                SendKeys.Send("{Tab}");
			}
		}

        private void txtTen_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTen_Validated(object sender, EventArgs e)
        {
            txtTen.Text = mDB.Caps(txtTen.Text);
        }

        private void cmdNhombc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_luu.Focus();
        }
       
        private void txtMa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txtsott_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txttenvt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}{F4}");
        }
        private string get_maxid()
        {
             int m_tam = 0;
            ds = mDB.get_data("SELECT Max(to_number(ma,999)) FROM " + mDB.user + ".dmcd where ma <> '99'");
            if (ds.Tables[0].Rows.Count > 0)
                m_tam = ds.Tables[0].Rows[0][0].ToString() !=""?int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1:1;
            else
                m_tam = 1;
            return m_tam.ToString();//.PadLeft(2, '0').ToString();
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            Ena_control(false);
            Ena_textbox(true);
            clear_Text();
            txtMa.Text = get_maxid();
            flag = 0;
            txtTen.Focus();
        }    
             
     
     
        private void btn_luu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_them.Focus();
        }

        private void btn_them_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtTen.Focus();
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Ena_control(false);
            Ena_textbox(true);
            txtTen.Focus();
        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            string nhomcb = "";
            int m_id = 0;
            decimal stt = 0;
            if (cmdNhombc.SelectedIndex != -1)
                nhomcb = cmdNhombc.SelectedValue.ToString();
            if (flag == 0)
            {
                m_id = Int32.Parse(get_maxid());
                txtMa.Text = m_id.ToString().PadLeft(2, '0');
            }
            else
            {
                if (txtMa.Text != "")
                    m_id = Int32.Parse(txtMa.Text.ToString());
            }
          

            try
            {
                mDB.upd_DMCHUCDANH(txtMa.Text, txtTen.Text, txttenvt.Text, cmdNhombc.SelectedValue.ToString(), stt);
                chucvu();
                flag = 0;
                Ena_control(true);
                Ena_textbox(false);
            }
            catch { }
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            Ena_control(true);
            Ena_textbox(false);
            refresh_Text();
            btn_thoat.Focus();
        }

        private void btn_huy_Click(object sender, EventArgs e)
        {
            try
            {
                int i = dgrDanhMuc1.CurrentCell.RowNumber;
                if (i < 0)
                    i = dgrDanhMuc1.CurrentCell.RowNumber;
                if (i == dgrDanhMuc1.CurrentCell.RowNumber)
                {
                    DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ chức vụ này?"),AccessData.Msg, MessageBoxButtons.YesNo);
                    if (dr == DialogResult.Yes && !kemtra(txtMa.Text.Trim()))
                    {
                        mDB.get_data("DELETE  FROM " + mDB.user + ".dmcd WHERE ma='" + txtMa.Text.Trim() + "'");
                    }
                    chucvu();
                    refresh_Text();
                }
            }
            catch { }
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        
     
	}
}
